package com.abel.bigwater.data.jnrq

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.model.JsonHelper
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.channel.embedded.EmbeddedChannel
import org.junit.Test
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat

class JnrqDecoderTests {
    @Test
    fun testDecodingFromJnrq() {
        val str = """68115546F1930000A1020100000F0000BDBD713729057302031620420181032E0C1507E2070000000000080000007402031620420181232E0C1507E2070000000000080000007502031620420181062F0C1507E2070000000000080000007602031620420181262F0C1507E207000000000008000000770203162042018109300C1507E207000000000008000000780203162042018129300C1507E20700000000000800000079020316204201810C310C1507E20700000000000800000080020316204201812C310C1507E20700000000000800000090020316204201810F320C1507E207000000000008000000910203162042018132320C1507E207000000000008000000920203162042018115330C1507E207000000000008000000930203162042018135330C1507E207000000000008000000940203162042018118340C1507E207000000000008000000950203162042018138340C1507E20700000000000800000096020316204201811B350C1507E20700000000000800000097020316204201813B350C1507E20700000000000800000098020316204201811E360C1507E207000000000008000000990203162042018102370C1507E207000000000008000000000303162042018121370C1507E207000000000008000000010303162042018131370C1507E207000000000008000000020303162042018115380C1507E207000000000008000000030303162042018134380C1507E207000000000008000000040303162042018118390C1507E207000000000008000000050303162042018137390C1507E20700000000000800000006030316204201811B3A0C1507E20700000000000800000007030316204201813A3A0C1507E20700000000000800000008030316204201811E3B0C1507E207000000000008000000C50D0A"""

        val str2 = """68 11 55 46 50 fd 00 00 39 01 01 00 00 18 00 00 43 43 45 54 59 09 19 58 00 50 10 33 78 82 17 37
01 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37 02 04 08 e4 07 00 24 13
00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37 03 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58
00 50 10 33 78 82 17 37 04 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37
05 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37 06 04 08 e4 07 00 24 13
00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37 07 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58
00 50 10 33 78 82 17 37 08 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37
09 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37 0a 04 08 e4 07 00 24 13
00 00 00 00 00 00 19 58 00 50 10 33 78 82 17 37 0b 04 08 e4 07 00 24 13 00 00 00 00 00 00 19 58
00 50 10 33 78 82 17 37 0c 04 08 e4 07 00 24 13 00 00 00 00 00 00 3b 0d 0a 
"""

        val channel = EmbeddedChannel(JnrqDecoder("11", "zn-", null), object : ChannelInboundHandlerAdapter() {
            override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {
                assert(msg is JnrqPacket)
                assert((msg as JnrqPacket).data is UploadHistoryData)

                val data = (msg as JnrqPacket).data as UploadHistoryData
                lgr.info("parsed result:\n${JSON.toJSONString(msg, true)}")

                val dateFormatter = SimpleDateFormat(JsonHelper.FULL_DATE_FORMAT)
                for (wd in data.waterList.orEmpty()) {
                    lgr.info("${wd.meterAddr} - ${dateFormatter.format(wd.sampleTime)}: ${wd.waterDigits}")
                }
            }
        })

        channel.writeInbound(Unpooled.buffer().writeBytes(BcdString.fromBcdString(str2).hexArray))

        channel.writeInbound(Unpooled.buffer().writeBytes(BcdString.fromBcdString(str3).hexArray))
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(JnrqDecoderTests::class.java)

        const val str3 = """68 11 55 46 aa e6 02 00 55 00 03 00 00 1b 00 00 09 09 7f 01 bb 05 95 16 00 09 10 42 01 81 37 0d
0d 16 01 e3 07 00 00 00 00 00 00 00 00 00 00 00 00 00 e5 d6 a5 fe 6d 7c 73 df bb 7c be b7 d6 f7
be 33 d6 5d ed b6 00 00 00 00 69 f3 4e f4 00 00 00 00 bc 0d 0a
"""
    }
}